﻿/**
 * Created by Administrator on 2017/3/1 0001.
 */
var arr_jsonGroup=[json_group0,json_group1];
var Preload=
{
    create:function()
    {
        var page={};
        var bool_threeLoaded;
        var bool_pixiLoaded;

        var loader_pre=new PIXI.loaders.Loader();
        var arr_assetsPre=[];
        arr_assetsPre.push(
        );
        var loader=new PIXI.loaders.Loader();


        var graphic_bg,graphic_loadingBottom,graphic_loading;
        var text_progress;
        var sp_logo;



        var arr_assets=[];
        var arr_exsit=[]
        arr_assets.push(
            'assets/images/创邑_LOGO.png'
        );

        // for(var i=0;i<arr_jsonGroup.length;i++)
        // {
        //     for(var name in arr_jsonGroup[i])
        //     {
        //         var bool_exsit;
        //         for(var j in arr_assets)
        //         {
        //             if(arr_assets[j]==arr_jsonGroup[i][name].url)
        //             {
        //                 bool_exsit=true;
        //                 break;
        //             }
        //         }
        //         if(!bool_exsit)
        //         {
        //             arr_assets.push(arr_jsonGroup[i][name].url);
        //             arr_exsit.push(arr_jsonGroup[i][name].url);
        //         }
        //     }
        // }


        // for(var i=0;i<62;i++)
        // {
        //     if(i<10)
        //     {
        //         arr_assets.push('assets/images/01/P1.00'+i.toString()+'.jpg')
        //     }
        //     else if(i<100)
        //     {
        //         arr_assets.push('assets/images/01/P1.0'+i.toString()+'.jpg')
        //     }
        //     else
        //     {
        //         arr_assets.push('assets/images/01/P1.'+i.toString()+'.jpg')
        //     }
        //
        // }
        var threeLoading=0;
        document.addEventListener('Three_Loading',function (e) {

            threeLoading=e.detail/2;

            text_progress.text=Math.floor(loader.progress/2+threeLoading)+"%";
            graphic_loading.scale.x=(loader.progress/2+threeLoading)/100;
            console.log( graphic_loading.scale.x)
        });
        document.addEventListener('ThreeLoaded',function () {
            bool_threeLoaded=true;
            if(bool_pixiLoaded&&bool_threeLoaded)
            {
                complete();
            }
        })
        
        loader.add(arr_assets)
            .on("progress", loadProgressHandler)
            .load(completeHandler);
        function loadProgressHandler(loader, resource)
        {
            text_progress.text=Math.floor(loader.progress/2+threeLoading)+"%";
            graphic_loading.scale.x=(loader.progress/2+threeLoading)/100;
            console.log( graphic_loading.scale.x)
        }
        function completeHandler()
        {
            bool_pixiLoaded=true;
            if(bool_pixiLoaded&&bool_threeLoaded)
            {
                complete();
            }
        }
        function complete()
        {
            var event = new CustomEvent("Main_LoadComplete", {detail:loader });
            document.dispatchEvent(event);
            TweenMax.to(graphic_bg,1,{alpha:0});
            TweenMax.to(sp_logo,1,{y:sp_logo.y-100,alpha:0,ease:Cubic.easeInOut});
            TweenMax.to(graphic_loading,1,{y:graphic_loading.y+100,alpha:0,ease:Cubic.easeInOut});
            TweenMax.to(graphic_loadingBottom,1,{y:graphic_loadingBottom.y+100,alpha:0,ease:Cubic.easeInOut});
            TweenMax.to(text_progress,1,{y:text_progress.y+100,alpha:0,ease:Cubic.easeInOut});
        }
        page.init=function(stage,stageW,stageH)
        {
            graphic_bg = new PIXI.Graphics();
            graphic_bg.beginFill(0x0);
            graphic_bg.drawRect(0,0,stageW,stageH);
            stage.addChild(graphic_bg);

            graphic_loadingBottom=new PIXI.Graphics();
            graphic_loadingBottom.beginFill(0xcccccc);
            graphic_loadingBottom.drawRect(0,0,300,2);
            graphic_loadingBottom.x=(stageW-graphic_loadingBottom.width)/2;
            graphic_loadingBottom.y=(stageH/2)+40;
            stage.addChild(graphic_loadingBottom);

            graphic_loading=new PIXI.Graphics();
            graphic_loading.beginFill(0xff6600);
            graphic_loading.drawRect(0,0,300,2);
            graphic_loading.x=(stageW-graphic_loading.width)/2;
            graphic_loading.y=(stageH/2)+40;
            graphic_loading.scale.x=0;
            stage.addChild(graphic_loading);

            sp_logo=new PIXI.Sprite.fromImage('assets/images/创邑_LOGO.png');
            sp_logo.anchor.set(0.5);
            sp_logo.x=stageW/2;
            sp_logo.y=stageH/2-50;
            stage.addChild(sp_logo);

            text_progress= new PIXI.Text('0%',{fontFamily : 'Arial', fontSize: 20, fill : 0xcccccc, align : 'center'});
            text_progress.anchor.x=0.5;
            text_progress.anchor.y=0.5;
            text_progress.x = stageW/2;
            text_progress.y = stageH/2+80;
            stage.addChild(text_progress);
        }

        return page;
    }
}
